package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleMapper;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.UuidUtil;
import com.itheima.vo.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 新增角色信息
     * @param roleVo
     */
    @Override
    public void add(RoleVo roleVo) {
        Role role = new Role();
        BeanUtils.copyProperties(roleVo,role);
        role.setUid(UuidUtil.getUuid());
        role.setStatus((byte) 1);
        role.setCreateTime(DateUtils.getNowDate());
        role.setUpdateTime(DateUtils.getNowDate());
        roleMapper.insertSelective(role);
    }

    /**
     * 删除角色信息
     * @param roleVo
     */
    @Override
    public void delete(RoleVo roleVo) {
        Role role = new Role();
        BeanUtils.copyProperties(roleVo, role);
        role.setUid(roleVo.getUid());
        role.setUpdateTime(DateUtils.getNowDate());
        role.setStatus((byte) 0);
        roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 更新角色信息
     * @param roleVo
     */
    @Override
    public void edit(RoleVo roleVo) {
        Role role = new Role();
        BeanUtils.copyProperties(roleVo, role);
        role.setUid(roleVo.getUid());
        role.setUpdateTime(DateUtils.getNowDate());
        roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 获取角色信息列表
     * @param keyword
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageResult getList(String keyword, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Map> mapList = roleMapper.findAllByKeyword(keyword);

        PageResult pageResult = new PageResult();
        pageResult.setRecords(mapList);
        pageResult.setTotal((long) mapList.size());
        pageResult.setCurrent(currentPage);
        pageResult.setSize(pageSize);

        return pageResult;
    }
}
